## 2020.04.16
## jun_军
## qq:604803231
## blog主页：https://blog.csdn.net/weixin_44868057
## qq交流群：635032789 （进群密码：词达人）







from aip import AipOcr
import json
import pyautogui
import numpy as np
import cv2 as cv
import time
import os

try:
    print("打开这个文件前要配置好fiddler \n下方输入的账号是百度ocr识别的密钥，具体怎么获得，看我的博客：https://blog.csdn.net/weixin_44868057/article/details/105523737")
    APP_ID = input('输入APP_ID：')  ##'19430759'
    API_KEY =  input('输入API_KEY：')                               ##'UTkLx2DGCuuHSFHFZadUc2vk'
    SECRET_KEY = input('输入SECRET_KEY：')


    while True:


        ##1截图以及图片处理
        print("\n---------------------")
        print("等待1s开始截图")

        img = pyautogui.screenshot(region=[37,130,385,850]) # x,y,w,h
        img.save('./1/answer.jpg')



        ## 2图片灰度处理
        # def access_pixels(image):
        #     height, width, channels = image.shape
        #     print("width:%s,height:%s,channels:%s" % (width, height, channels))
        #
        #     for row in range(height):
        #         for list in range(width):
        #             for c in range(channels):
        #                 pv = image[row, list, c]
        #                 image[row, list, c] = 255 - pv
        #     # cv.imshow("AfterDeal", image)
        #
        #
        # src = cv.imread('./1/answer.jpg')
        #     # cv.imshow('OriginalImage', src)
        #
        #     # 获取当前系统以运行的周期数
        # t1 = cv.getTickCount()
        #     # 运行函数
        # access_pixels(src)
        #     # 再次获取当前系统以运行的周期数
        # t2 = cv.getTickCount()
        #     # 计时周期数差值除以每秒周期数，获得处理时间
        # print("time cost:%s s" % ((t2 - t1) / cv.getTickFrequency()))
        # cv.imwrite('2/answer.jpg',src)

        ## 3图片放大10倍
            # 已知数据集位置
        indir = "./1/"

            # 保存的数据集位置
        outdir = "./3/"

        cout = 0
        for fileName in os.listdir(indir):
            if fileName == '.DS_Store':
                continue
            cout = cout + 1
            # print(cout)
            # print(fileName)
            img_dir = os.path.join(indir, fileName)  # 获取图片的名字
            # print(img_dir)
            src = cv.imread(img_dir)  # 读取图片
            h1, w1 = src.shape[:2]
            # print(h1, w1)  # 打印长和宽
            r = 1/3 # 缩小倍数
            # 进行缩放
            dst = cv.resize(src, (int(w1 / r), int(h1 / r)), interpolation=cv.INTER_CUBIC)  # 使用双立方插值
            h2, w2 = dst.shape[:2]
            # print(h2, w2)  # 打印长和宽
            # print(outdir + fileName)
            cv.imwrite(outdir + fileName, dst)


            ## 4图片显示增强
            I = cv.imread('./1/answer.jpg', flags=0)
            aa = 2
            Oa = I * float(aa)
            Oa[Oa > 255] = 255
            Oa = np.round(Oa)
            Oa = Oa.astype(np.uint8)
            cv.imwrite('./4/answer.jpg', Oa)






        ##5.解析本地json文件答案
        type = 0
        lodec = 'responseBody.txt'
        # def getAnswer():
        #     print('搜寻答案....')


        answers = []
        data = json.loads(open(lodec, "r", encoding='utf-8').read())
        # 多选
        if data['data']['topic_mode'] in [31]:
            options = data['data']['options']
            for option in options:
                if option['answer'] == True:
                    answers.append(option['content'].replace('adj','').replace('adv','').replace('noun','').replace(';','').replace(',','').replace('adj','').replace('adv','')
                                   .replace('noun','').replace('verb','').replace('ver','').replace('ad','').replace('f','').replace('verb','').replace('j','')
                                   .replace('l','').replace('J','').replace('d','').replace('i','').replace('S','s').replace('W','w').replace('K','k')\
                .replace('J','j').replace('C','c').replace('X','x').replace('y','').replace('尽管','').replace('密','').replace('空','').replace('g','').replace('prep','').replace('Z','z').replace('a','').replace("期",'').replace('s','').replace('I',''))
        # 单选
        if data['data']['topic_mode'] in [11, 22, 42]:
            options = data['data']['options']
            for option in options:
                if option['answer'] == True:
                    answers.append(
                        option['content'].replace(' ', '').replace('；', ';').replace('，', ',').replace('.', '').replace('…',
                               '').replace('adj','').replace('adv','').replace('noun','').replace('verb','').replace(';','')
                            .replace('adj','').replace('adv','').replace('f','').replace('noun','').replace('verb','').replace('ver','')
                            .replace('ad','').replace(',','').replace('d','').replace('写','').replace('j','').replace('S','s').replace('W','w').replace('K','k')\
                .replace('J','j').replace('C','c').replace('X','x').replace('y','').replace('尽管','').replace('密','').replace('空','').replace('g','').replace('prep','').replace('Z','z').replace("期",'').replace('a','').replace('l','').replace('J','').replace('s','').replace('i','').replace('I',''))
        # 选词
        if data['data']['topic_mode'] in [32]:
            options = data['data']['answer_content']['answer_arr']
            for option in options:
                # print(option)
                answers.append(option.replace('\'','').replace('j','').replace('l','').replace('J','').replace('i','').replace('adj','')
                               .replace('adv','').replace('I','').replace('f','').replace('d','').replace("期",'').replace('S','s').replace('W','w').replace('K','k')\
                .replace('J','j').replace('C','c').replace('X','x').replace('y','').replace('g','').replace('prep','').replace('Z','z').replace('noun','').replace('verb','').replace('s','').replace('a','').replace('ver','').replace('ad',''))


        # 填空
        if data['data']['topic_mode'] in [51]:
            type = 1
            answers.append(data['data']['answer_content'])
        for a in answers:
            print(a)


            # return answers



        print("答案：",answers)


        ##6.ocr识别

        # m=0
        # if m<=300:

        """ 你的 APPID AK SK  图2的内容"""
        # APP_ID = '11206246'
        # API_KEY = 'Xdp8WvFlZfG7eGQ54vlTUOc3'
        # SECRET_KEY = '2AEFzhIp4TF8c3xzLpWOqqFE1p25K22f'

        # APP_ID = input('输入APP_ID：')         ##'19430759'
        # API_KEY = 'UTkLx2DGCuuHSFHFZadUc2vk'
        # SECRET_KEY = 'eGfDmWyYSbi6YLpq0nC5hDkYdMmrpouH'

        # APP_ID = '19453795'
        # API_KEY = 'Aqz3lZgWO1S29TmLtz7Xqpmw'
        # SECRET_KEY = '9wtFKf5eLpi5xrDanWnS0YYgmDHmoSIN'
        # elif 300<m<600:
        #     APP_ID = '19456426'
        #     API_KEY = 'xFtgeM8aeGyw16zixClN8s2G'
        #     SECRET_KEY = 'zQCNclMma0XyBYtOjVKlqQGtAw4rfc6K'


        client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


        fname = './4/answer.jpg'
        # fname = 'screenshot.png'
        # """ 读取图片 """
        def get_file_content(filePath):
            with open(filePath, 'rb') as fp:
                return fp.read()
        #
        image = get_file_content(fname)

        """ 调用通用文字识别, 图片参数为本地图片 """
        client.general(image)

        # options = {}
        # options["recognize_granularity"] = "big"
        # options["language_type"] = "CHN_ENG"
        # options["detect_direction"] = "true"
        # options["detect_language"] = "true"
        # options["vertexes_location"] = "true"
        # options["probability"] = "true"


        results = client.trainTicket(image)["words_result"] # 还可以使用身份证驾驶证模板，直接得到字典对应所需字段


        h = []
        t = []
        for result in results:
            text = result["words"].replace(' ','').replace('adj','').replace('adv','').replace('I','').replace('gan√','gain').replace("I","")\
                .replace('j','').replace('J','').replace('i','').replace('a','').replace('veb','').replace('…','')\
                .replace('noun','').replace('verb','').replace('veb','').replace('g','').replace('o愚','愚').replace('V','v').replace('ver','').replace('ana','an').replace('.','').replace('ad','')\
                .replace(';','').replace(',','').replace('写','').replace('y','').replace('l','').replace('f','').replace('d','').replace("期",'').replace('S','s').replace('W','w').replace('K','k')\
                .replace('J','j').replace('C','c').replace('0','o').replace('尽管','').replace('ntnt','nnt').replace('密','').replace('空','').replace('\'','').replace('X','x')\
                .replace(':','').replace('omey','omeboy').replace('prep','').replace('pep','').replace('outpu','output').replace('Z','z').replace('斤','').replace('s','')\
                .replace('veb','').replace('nown,''')
            b= (result["location"]['left']/int('3')+int('50'),result["location"]['top']/int('3')+int('140'))
            h.append(text)
            t.append(b)


            # e = (text)
            # text = print(result["words"].replace(';', '')) # 只有中文英文
            #
            # # f = print(a.replace('None',''))     #
            # location = result["location"]
            # # a= [result["location"]['left'],result["location"]['top']]
            # c= list(text)+list(b)
            # text1=list(text)+list(b)
            #
            # # # print(re.findall(r"\d+\.?\d*",text))
            # # content = list("".join(a)) + list(b)
            # t=[]

        # print('识别到的坐标：',t)
        print("识别的：",h)

        if len(answers)>200:

            break
        #
        # if  len(list(h)) <100:
        # tmp = [val for val in h if val in answers]  # 列表推导式求的两个列表的交集

        #     pass
        ## 已知的答案与识别到的进行对比，然后将坐标赋值给我们要的答案
        else:
            i=0
            q=0
            zuobiao =[]
            while i < len(answers):
                p= h.index(answers[i])
                zuobiao = list(t[p])
                # i +=1
                # print(zuobiao)

                x=int(zuobiao[0])  #+ int('94')

                y=(zuobiao[1])   #+ int('94')
                # print(x)
                # print(y)

                # PyMouse.click(x,y)
                # pyautogui.moveTo(x, y, duration=1)
                pyautogui.click(x, y)
                i += 1
                time.sleep(0.5)

            # time.sleep(1)
            k = 0
            while k<5:
                pyautogui.click(400, 1000)
                time.sleep(1)
                k +=1
                # time.sleep(1)

            time.sleep(1.5)
except:
    print("情况1：仔细看我的博客，和前面的1-4点内容创建文件夹，\n情况2::当前的页面不在答题界面 \n情况3：如果出现答案与识别到的不一样，可以截图发给604803231@qq.com邮箱  \n4.如果因为一道题二跳出自动，那么就把这道题自己答，答完后在程序运行本程序")





